package com.linhh.mall.center.web.controller;

import com.github.pagehelper.PageInfo;
import com.linhh.mall.center.api.GoodsInfoService;
import com.linhh.mall.center.common.pojo.*;
import com.linhh.mall.center.common.util.HttpResult;
import com.linhh.mall.center.web.util.FastDFSClientUtil;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品信息控制层
 * @Author linhh
 * @Date 2023 02 21 02 49
 **/
@RestController
@RequestMapping("api")
public class GoodsInfoController {

    @Reference
    private GoodsInfoService goodsInfoService;

    @Resource
    private FastDFSClientUtil fastDFSClientUtil;

    /**
     * 查询商品信息列表
     * @param queryCondition
     * @return
     */
    @PostMapping(value = "/queryGoodsList")
    public HttpResult queryGoodsList(@RequestBody GoodsQueryCondition queryCondition){
       PageInfo<GoodsInfo> goods = goodsInfoService.queryAllByCondition(queryCondition);
       return HttpResult.success(goods);
    }

    /**
     * 添加
     * @param tbProduceInfoVo
     * @return
     */
    @ResponseBody
    @PostMapping("/addProduct")
    public HttpResult addTbProduct(@RequestBody ProduceInfoVo tbProduceInfoVo){
        ProduceInfoVo produceInfoVo = (ProduceInfoVo) tbProduceInfoVo;
        int result = goodsInfoService.addProductInfo(produceInfoVo,tbProduceInfoVo.getExtrAttribute(),tbProduceInfoVo.getIntroduce());
        return HttpResult.success(result);
    }

    /**
     * 静态页面
     * @return
     */
    @GetMapping("/buildPage")
    public HttpResult staticPageBuild(GoodsInfo info){
        String result ="OK";
        goodsInfoService.staticPageBuild(info);
        return HttpResult.success(result);
    }

    /**
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public HttpResult uploadFile (@RequestParam("file") MultipartFile file){
        String result = null;
        try{
            String path = fastDFSClientUtil.uploadImage(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上传失败" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return HttpResult.success(result);
    }

    /**
     *
     * @param fileUrl
     * @return
     */
    @RequestMapping(value = "/deleteImage", method = RequestMethod.GET)
    public HttpResult downloadFile (@RequestParam("fileUrl") String fileUrl){
        String result = null;
        try{
            fastDFSClientUtil.deleteFile(fileUrl);
            result = "success" ;
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return HttpResult.success(result);
    }

    /**
     *
     * @param map
     * @return
     */
    @ResponseBody
    @PostMapping("/relatedImg")
    public HttpResult relateImage(@RequestBody Map<String,Object> map){
        Long goodsId = Long.valueOf((Integer) map.get("goodsId"));
        List<String> imgs = (List<String>) map.get("img");
        // 先删除当前id下的所有图片
        goodsInfoService.deleteImgByGoodsId(goodsId);
        GoodsAttach goodsAttach = new GoodsAttach();
        goodsAttach.setGoodsId(goodsId);
        int result = 0;
        for (String img : imgs){
            goodsAttach.setPath(img);
            goodsInfoService.relatedImg(goodsAttach);
            result++;
        }
        return HttpResult.success(result);
    }

    /**
     *
     * @param goodsId
     * @return
     */
    @ResponseBody
    @GetMapping("/getImg")
    public HttpResult getImg(Long goodsId){
        List<GoodsAttach> list = goodsInfoService.getImgs(goodsId);
        List<String> arrayList = new ArrayList<>();
        for (GoodsAttach goodsAttach : list) {
            arrayList.add(goodsAttach.getPath());
        }
        return HttpResult.success(arrayList);
    }

    /**
     * 更新商品信息
     * @param info
     * @return
     */
    @PutMapping(value = "/StoreProduct")
    public HttpResult update(@RequestBody GoodsInfo info){
        int result = goodsInfoService.updateGoods(info);
        return HttpResult.success(result);
    }

}
